/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.facade.employment;

import com.xtd.facade.AbstractFacade;
import com.xtd.model.employment.DepartmentPost;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author Lu Yuanbin
 */
@Stateless
public class DepartmentPostFacade extends AbstractFacade<DepartmentPost> {

    public DepartmentPostFacade() {
        super(DepartmentPost.class);
    }

    public List<String> findAllDepartments() {
        return getEntityManager().createQuery("select distinct o.name from DepartmentPost as o").getResultList();
    }

    public List<DepartmentPost> findByDepartment(String department) {
        return getEntityManager().createQuery("select o from DepartmentPost as o where o.name = :name").setParameter("name", department).getResultList();
    }

    public boolean hasDuplicateDepartmentPost(DepartmentPost departmentPost, boolean isNew) {
        List l = null;
        if (isNew) {
            l = getEntityManager().createQuery("select o from DepartmentPost as o where o.name = :name and o.post = :post").setParameter("name", departmentPost.getName()).setParameter("post", departmentPost.getPost()).getResultList();
        } else {
            l = getEntityManager().createQuery("select o from DepartmentPost as o where o.name = :name and o.post = :post and o <> :departmentPost ").setParameter("name", departmentPost.getName()).setParameter("post", departmentPost.getPost()).setParameter("departmentPost", departmentPost).getResultList();
        }
        return l.size() > 0;
    }

    public List<DepartmentPost> findAllHasAditionalRoyalty() {
        return getEntityManager().createQuery("select o from DepartmentPost as o where o.addtionalSelectRoyalty is true").getResultList();
    }

    public void update(DepartmentPost dp) {
        if (dp.getId() == null) {
            create(dp);
        } else {
            edit(dp);
        }
    }
}
